{extend name="public:base2"/}
{block name="main"}
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <div class="layui-row">
    <div class="layui-col-md6">
        <ul id="menu"></ul>
      
        <br/>
        <button class="layui-btn" type="button" id="menumake">立即生成</button>

    </div>
    <div class="layui-col-md6">
  
        <!-- 编辑菜单 -->

        <form class="layui-form" action="" id="menuinfo"  >
        <label class="layui-form-label"><b>菜单信息</b></label>
          <input type="hidden" name="id" value="">
          <div class="layui-form-item">
            <label class="layui-form-label">上级菜单</label>
            <div class="layui-input-block">
              <select name="pid">
              <option value="">请选择上级菜单</option>
              {foreach $list as $v}
                <option value="{$v[id]}" >{$v['name']}</option>
              {/foreach}

            </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
              <input type="text" name="name"  placeholder="请输入菜单名称" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">事件</label>
            <div class="layui-input-block">
              
              <select name="type">
                  <option value="">请选择事件类型</option>
                  <option value="click">点击事件</option>
                  <option value="view">跳转链接</option>
              </select>
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">事件值</label>
            <div class="layui-input-block">
              <input type="text" name="val"  placeholder="请输入事件值"  class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-block">
              <button class="layui-btn" lay-submit="" >保存</button>
              <button type="reset" class="layui-btn layui-btn-primary">重置</button>
              <button type="button" class="layui-btn layui-btn-primary" data-id="" id="menudel" style="display:none;">删除</button>
            </div>
          </div>
        </form>
       
    </div>
  </div>


<script type="text/javascript">
	layui.use(['form','tree','layer'], function(){

    var $=layui.$;

	  layui.tree({
      elem: '#menu' //传入元素选择器
      ,nodes: {$menu}
      ,click: function(node){
        $("#menuinfo button[type='reset']").hide();
        $("#menudel").show();
        console.log(node) //node即为当前点击的节点数据

        $("#menudel").attr("data-id",node.id);
        
        $("#menuinfo select[name='pid']").val(node.pid);
        if(node.pid=='0'){
          $("form .layui-form-item:eq(0)").hide();
        }else{
          $("form .layui-form-item:eq(0)").show();
        }

        $("#menuinfo input[name='id']").val(node.id);
        $("#menuinfo input[name='name']").val(node.name);
        $("#menuinfo select[name='type']").val(node.type);
        $("#menuinfo input[name='val']").val(node.val);

        layui.form.render('select')
      }  
    });

    //菜单删除
    $("#menudel").click(function(){
        var id=$(this).attr("data-id");
          layer.confirm('是否删除当前菜单以及子菜单？', function(index){

           $.post("{:Url('site/menudel')}",{id:id},function(data){
              alert(data,'1');
           });

        }); 
    });
    //菜单添加/编辑
    $("#menuinfo").submit(function(){
      var id=$("#menuinfo input[name='id']").val();
      var pid=$("#menuinfo select[name='pid']").val();
      var name=$("#menuinfo input[name='name']").val();
      var type=$("#menuinfo select[name='type']").val();
      var val=$("#menuinfo input[name='val']").val();
      if(name==''){
        alert("请输入菜单名称~");
     
      }else if(type==''){
        alert("请选择事件类型~");
      
      }else if(val==''){
        alert("请输入事件值~");
        
      }else{
        $.post(location.href,{name:name,type:type,val:val,pid:pid,id:id},function(data){
            if(data=='ok'){
                alert("","1");
            }else{
                alert(data,"1");
            }
        });


      }

      return false;
       
    });

    /*菜单生成*/
    $("#menumake").click(function(){
          layer.confirm('是否立即生成菜单？', function(index){
           layui.layer.closeAll(); 
           layui.layer.load(); 
           $.post("{:Url('site/menumake')}",{},function(data){
              layui.layer.closeAll('loading');
               alert(data,'1');
           });

        }); 
    });
   
    
	});



</script>
<script>
/*$(function () {
   
    var um = UM.getEditor('myEditors');
});*/
</script>

{/block}